

/*
rolling regressions of various measures of asset prices at auctions

output:
- ../data/rolling/b_*_shock.dta: output from rolling regressions
- f2c_rolling_regressions.pdf: figure of rolling regressions

*/


* plotting options
set scheme lean_uncluttered

* command line options
* set to 1 to re-estimate rolling regressions (slow)
if "`1'"=="" {
	local rerun_rolling_intraday 0
}
else {
	local rerun_rolling_intraday `1'
}
if "`2'"=="" {
	local rerun_rolling_daily 0
}
else {
	local rerun_rolling_daily `2'
}
if "`3'"=="" {
	local rerun_rolling_ffr 0
}
else {
	local rerun_rolling_ffr `3'
}



********************************************************************************
********************************************************************************
* run rolling regressions
if `rerun_rolling_intraday' {
n di "intraday rolling regressions"

* merge intraday TAQ shocks
use ../data/auction_notesbonds_govpx_shock.dta, clear
rename auctionDate date
rename cusip auction_cusip
* intraday LQD etf
merge 1:1 date auction_cusip using ../data/input/taq_shock.dta, nogen keep(match)

* for newey-west SEs
sort release_time
gen _T = _n
tsset _T

* Rolling regressions: 75 auctions (roughly 1-2 years)
local taq_tickers LQD GLD SPY IWM
* rescale shocks to log points, and create label
local taq_tickers LQD GLD SPY IWM
foreach tic of local taq_tickers {
	replace D_shock`tic' = 100*D_shock`tic'
	label var D_shock`tic' "`tic'"
}

foreach var of local taq_tickers {
    n di "`var'"
	rolling _b _se e(N), ///
        window(75) keep(date) nodots ///
        saving(../data/rolling/b_`var'_shock.dta, replace) : ///
        ivreg2 D_shock`var' D_shock, r bw(9)
}

}

********************************************************************************
********************************************************************************
if `rerun_rolling_daily' {
n di "daily rolling regressions"

* merge daily FRED and Datastream shocks
use ../data/auction_notesbonds_govpx_shock.dta, clear
rename auctionDate date
* note: for daily data, only keep later auction if multiple occur in one day
* only a handful of multiple auctions
bys date (close_time): gen _idx_keep = _n==_N
keep if _idx_keep
drop _idx_keep

* daily FRED data
merge 1:1 date using ../data/fred/fred_daily_shocks.dta, ///
	keep(master match) nogen

* daily Datastream data
merge m:1 date using ../data/input/datastream_daily_shocks.dta, ///
	keep(master match) nogen

* for newey-west SEs
sort release_time
gen _T = _n
tsset _T

* variables to compute rolling regressions
local rolling_vars D_MD_AAA Dlog_PSPCOMP Dlog_PFRUSSL2 ///
	D_PICAIS10 D_PICAIS2Y Dlog_PGSCITOT ///
	D_MD_AAA_BAA D_LIBOR_OIS_spr 
* rescale to basis/log points
ds `rolling_vars'
foreach var of varlist `r(varlist)' {
	replace `var' = 100*`var'
}

foreach var in `rolling_vars' {
    n di "`var'"
    rolling _b _se e(N), ///
        window(75) keep(date) nodots ///
        saving(../data/rolling/b_`var'_shock.dta, replace) : ///
        ivreg2 `var' D_shock, r bw(9)
}


}


********************************************************************************
********************************************************************************
if `rerun_rolling_ffr' {
n di "daily fed funds futures rolling regressions"

* merge daily fed funds futures
use ../data/auction_notesbonds_govpx_shock.dta, clear
rename auctionDate date
* note: for daily data, only keep later auction if multiple occur in one day
* only a handful of multiple auctions
bys date (close_time): gen _idx_keep = _n==_N
keep if _idx_keep
drop _idx_keep

* daily fed funds futures data
merge 1:1 date using ../data/input/fed_funds_futures_continuous.dta, ///
	keep(master match) nogen

* for newey-west SEs
sort release_time
gen _T = _n
tsset _T

* loop over contract horizon
forval i=0/12 {
    n di "ffr_cont_`i'm shock"
    rolling _b _se e(N), ///
        window(75) keep(date) nodots ///
        saving(../data/rolling/b_ffr_cont_`i'm_shock.dta, replace) : ///
        ivreg2 ffr_cont_`i'm_interday D_shock, r bw(9)
	* also compute average volume
	rolling _b e(N), window(75) keep(date) nodots ///
		saving(../data/rolling/b_ffr_cont_`i'm_vol.dta, replace) : ///
		reg ffr_cont_`i'm_vol
}


}




********************************************************************************
********************************************************************************
* plot rolling coefficients


* subset of assets
local rolling_vars LQD GLD SPY IWM ///
	D_MD_AAA Dlog_PSPCOMP D_PICAIS10 Dlog_PGSCITOT D_MD_AAA_BAA
local rolling_titles LQD GLD SPY IWM ///
	"Aaa" "SP500" "10Y Infl. Swap" "GSCI" "Aaa-Baa"

* date range: 1997 to 2017; keep axis tight
local xmin `=mdy(1,1,1997)'
local xmax `=mdy(12,31,2017)'
local xlabels `=mdy(1,1,1997)' `=mdy(1,1,2002)' `=mdy(1,1,2007)' ///
	`=mdy(1,1,2012)' `=mdy(1,1,2017)' 

local n: word count `rolling_vars'
forvalues i=1/`n' {
	local var: word `i' of `rolling_vars'
	local var_title: word `i' of `rolling_titles'
	* load rolling regression results
    n di "`var'"
    use ../data/rolling/b_`var'_shock.dta, clear
    rename _eq2_stat_1 N
    drop if N<20 | N==.
    gen ci_l = _b_D_shock - 2*_se_D_shock 
    gen ci_u = _b_D_shock + 2*_se_D_shock
	
	* year-only date format for plots
	format date %tdCY
	* only label left figures y-axis
	if inlist("`var'", "LQD", "SPY", "GLD") {
		local y_axis_str ytitle("Coefficient on D{sub:t}")
	}
	else local y_axis_str ytitle("")
	
	twoway ( rarea ci_l ci_u date, color(gs12) ) ///
		( line _b_D_shock date, ///
			lcolor("scheme p1") lstyle("scheme p1lineplot") ), ///
		legend(off) ///
		xscale(r(`xmin' `xmax')) xlabel(`xlabels') ///
        xtitle("") `y_axis_str' ///
		title("`var_title'") name(rolling_`var', replace) nodraw
	
}
* combine with correct size and export
local fname f2c_rolling_regressions
graph combine rolling_LQD rolling_D_MD_AAA rolling_D_MD_AAA_BAA ///
	rolling_SPY rolling_IWM rolling_Dlog_PSPCOMP ///
	rolling_GLD rolling_Dlog_PGSCITOT rolling_D_PICAIS10, ///
	rows(3) imargin(tiny) xsize(8) ysize(5) name(`fname', replace)
graph export ../figs/`fname'.pdf, replace
graph export ../figs/`fname'.eps, replace



* subset of fed funds futures
* date range: 1997 to 2017; keep axis tight
local xmin `=mdy(1,1,1997)'
local xmax `=mdy(12,31,2017)'
local xlabels `=mdy(1,1,1997)' `=mdy(1,1,2002)' `=mdy(1,1,2007)' ///
	`=mdy(1,1,2012)' `=mdy(1,1,2017)' 

foreach i of numlist 0 1 3 6 9 12 {
    n di "ffr_cont_`i'm shock"
	local var_title "`i'-Month Ahead"
	* load rolling regression results
    use ../data/rolling/b_ffr_cont_`i'm_shock.dta, clear
    rename _eq2_stat_1 N
    drop if N<20 | N==.
    gen ci_l = _b_D_shock - 2*_se_D_shock 
    gen ci_u = _b_D_shock + 2*_se_D_shock
	
	* combine volume data
	drop _b_cons
	merge 1:1 date using ../data/rolling/b_ffr_cont_`i'm_vol.dta, ///
		keep(master match) keepusing(_b_cons)
	rename _b_cons _b_vol
	replace _b_vol = _b_vol/1000
	
	* year-only date format for plots
	format date %tdCY
	* only label left figures y-axis/right figures y-axis2
	local y_axis_str ytitle("")
	local y_axis_str2 ytitle("", axis(2))
	if inlist(`i', 0, 6) {
		local y_axis_str ytitle("Coefficient on D{sub:t}")
	}
	if inlist(`i', 3, 12) {
		local y_axis_str2 ytitle("Trade Volume (Thousands)", axis(2))
	}
	* comparable y-axes
	if `i'<=3 {
		local ymin -1
		local ymax 1
		local ylabels -1 -0.5 0 0.5 1
	}
	else {
		local ymin -3
		local ymax 3
		local ylabels -3 -1.5 0 1.5 3
	}
	
	twoway ( rarea ci_l ci_u date, color(gs12) ) ///
		( line _b_D_shock date, ///
			lcolor("scheme p1") lstyle("scheme p1lineplot") ) ///
		(line _b_vol date, ///
			lcolor("scheme p2") yaxis(2) lp("scheme p2lineplot")), ///
		legend(off) ///
		xscale(r(`xmin' `xmax')) xlabel(`xlabels') ///
		yscale(r(`ymin' `ymax')) ylabel(`ylabels') ///
        xtitle("") `y_axis_str' `y_axis_str2' ///
		title("`var_title'") name(rolling_ffr`i', replace) nodraw
	
}
* combine with correct size and export
local fname f2c_rolling_ffr_regressions
graph combine rolling_ffr0 rolling_ffr1 rolling_ffr3 ///
	rolling_ffr6 rolling_ffr9 rolling_ffr12, ///
	rows(2) imargin(tiny) xsize(8) ysize(5) name(`fname', replace)
graph export ../figs/`fname'.pdf, replace
graph export ../figs/`fname'.eps, replace


